package com.hengxinguotong.zhihuichengjian.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.hengxinguotong.zhihuichengjian.R;
import com.hengxinguotong.zhihuichengjian.bean.Message;
import com.hengxinguotong.zhihuichengjian.widget.font.HXTextView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/7/4.
 */

public class HomeMessageListAdapter extends BaseAdapter {

    private List<Message> messageList;
    private LayoutInflater inflater;

    public HomeMessageListAdapter(List<Message> messageList, Context context) {
        this.messageList = messageList;
        this.inflater = LayoutInflater.from(context);
    }

    public void setMessageList(List<Message> messageList) {
        this.messageList = messageList;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return messageList.size();
    }

    @Override
    public Object getItem(int position) {
        return messageList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if(null == convertView || null == convertView.getTag()){
            convertView = inflater.inflate(R.layout.item_home_message, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder)convertView.getTag();
        }
        Message message = messageList.get(position);
        holder.item_time.setText(message.getCreateTime());
//        if(message.getContent().length() > 12){
//            holder.item_title.setText(message.getContent().substring(0, 11) + "...");
//        }else{
            holder.item_title.setText(message.getContent());
//        }
        return convertView;
    }

    static class ViewHolder{
        @BindView(R.id.item_title)
        HXTextView item_title;
        @BindView(R.id.item_time)
        HXTextView item_time;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
